import Vue from 'vue'
import VueRouter from 'vue-router'
import loginPage from '../page/loginPage/loginPage.vue'
import homePage from '../page/homePage/homePage.vue'
import personInfo from '../page/personInfo'
import homeInfo from '../page/homeInfo'
import gradeShow from '../page/gradeShow'
import changePassword from '../page/changePassword'
import queryGrade from '../page/queryGrade'
import queryGradeAll from '../page/queryGradeAll'
import gradeShowAll from '../page/gradeShowAll'
import personInfoAll from '../page/personInfoAll'
Vue.use(VueRouter)


//配置多次点击不会报错
let originPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        originPush.call(this.addRoute, location, resolve, reject)
    } else {
        originPush.call(this, location, () => { }, () => { })
    }
}

const router = new VueRouter({
    scrollBehavior() {
        return { y: 0 }
    },
    routes: [
        {
            component: loginPage,
            path: '/loginPage',
            name: 'loginPage',
        },
        {
            path: '/',
            redirect: '/loginPage'
        },
        {
            component: homePage,
            path: '/homePage',
            name: 'homePage',
            children: [
                {
                    component: personInfo,
                    path: '/personInfo',
                    name: 'personInfo',
                },
                {
                    component: changePassword,
                    path: '/changePassword',
                    name: 'changePassword',
                },

                {
                    component: homeInfo,
                    path: '/homeInfo',
                    name: 'homeInfo',
                },
                {
                    component: gradeShow,
                    path: '/gradeShow',
                    name: 'gradeShow',
                },
                {
                    component: queryGrade,
                    path: '/queryGrade',
                    name: 'queryGrade',
                },
                {
                    component: queryGradeAll,
                    path: '/queryGradeAll',
                    name: 'queryGradeAll',
                },
                {
                    component: gradeShowAll,
                    path: '/gradeShowAll',
                    name: 'gradeShowAll',
                },
                {
                    component: personInfoAll,
                    path: '/personInfoAll',
                    name: 'personInfoAll',
                },
            ]
        },

    ]
})


//路由守卫(前置守卫)
router.beforeEach((to, from, next) => {
    const token = sessionStorage.getItem('token')
    if (!token) {
        if (to.path == '/loginPage') {
            next()
        } else {
            next(false)
        }
    } else {
        if (to.path == '/loginPage') {
            next(false)
        } else {
            next()
        }
    }
})

export default router